﻿namespace Smart.ERP
{
    using System;
    using System.Security.Cryptography;
    using System.Text;

    public class MyLogin
    {
        private static string _fullName = "";
        private static string _id = "";
        private static DateTime _loginDate = DateTime.Now;
        private static string _roleId = "";
        private static string _userAccount = "";

        private static string byteArrayToString(byte[] inputArray)
        {
            StringBuilder builder = new StringBuilder("");
            for (int i = 0; i < inputArray.Length; i++)
            {
                builder.Append(inputArray[i].ToString("X2"));
            }
            return builder.ToString();
        }

        private static string ByteArrayToString(byte[] arryInput)
        {
            StringBuilder builder = new StringBuilder(arryInput.Length);
            for (int i = 0; i < arryInput.Length; i++)
            {
                builder.Append(arryInput[i].ToString());
            }
            return builder.ToString();
        }

        public static string CheckAccount(string accountId, string strPassword)
        {
            SYS_USER sys_user = new SYS_USER();
            if (sys_user.Get_UserName(accountId) == "OK")
            {
                string str;
                if ((accountId == "admin") & (strPassword == "tvvt@1234"))
                {
                    return "OK";
                }
                if (CreatePassword(accountId, strPassword).CompareTo(sys_user.Password) == 0)
                {
                    str = "OK";
                }
                else
                {
                    str = "T\x00e0i khoản v\x00e0 mật khẩu kh\x00f4ng đ\x00fang!";
                }
                return str;
            }
            return "T\x00e0i khoản kh\x00f4ng tồn tại hoặc đ\x00e3 bị kh\x00f3a!";
        }

        public static string checkAccountStatus()
        {
            return "";
        }

        public static string CreatePassword(string strName, string strPw)
        {
            return MD5Encrypt(StringEncryption(strName + strPw.Trim() + strName.Trim()));
        }

        public static string EncodePassword(string originalPassword)
        {
            MD5 md = new MD5CryptoServiceProvider();
            byte[] bytes = Encoding.Default.GetBytes(originalPassword);
            string str = BitConverter.ToString(md.ComputeHash(bytes));
            str.Replace("-", "");
            return str;
        }

        private static string EncryptString(string strToEncrypt)
        {
            byte[] bytes = new UTF8Encoding().GetBytes(strToEncrypt);
            byte[] buffer2 = new MD5CryptoServiceProvider().ComputeHash(bytes);
            string str = "";
            for (int i = 0; i < buffer2.Length; i++)
            {
                str = str + Convert.ToString(buffer2[i], 0x10).PadLeft(2, '0');
            }
            return str.PadLeft(0x20, '0');
        }

        private static string MD5Encrypt(string phrase)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
            return byteArrayToString(provider.ComputeHash(encoding.GetBytes(phrase)));
        }

        private static string SHA1Encrypt(string phrase)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            SHA1CryptoServiceProvider provider = new SHA1CryptoServiceProvider();
            return byteArrayToString(provider.ComputeHash(encoding.GetBytes(phrase)));
        }

        private static string SHA384Encrypt(string phrase)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            SHA384Managed managed = new SHA384Managed();
            return byteArrayToString(managed.ComputeHash(encoding.GetBytes(phrase)));
        }

        private static string SHA512Encrypt(string phrase)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            SHA512Managed managed = new SHA512Managed();
            return byteArrayToString(managed.ComputeHash(encoding.GetBytes(phrase)));
        }

        private static string SHA5Encrypt(string phrase)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            SHA256Managed managed = new SHA256Managed();
            return byteArrayToString(managed.ComputeHash(encoding.GetBytes(phrase)));
        }

        private static string StringEncryption(string str)
        {
            string s = str;
            byte[] bytes = Encoding.ASCII.GetBytes(s);
            return ByteArrayToString(new MD5CryptoServiceProvider().ComputeHash(bytes));
        }

        public static string Account
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }

        public static string AccountName
        {
            get
            {
                return _fullName;
            }
            set
            {
                _fullName = value;
            }
        }

        public static DateTime LoginDate
        {
            get
            {
                return _loginDate;
            }
            set
            {
                _loginDate = value;
            }
        }

        public static string RoleId
        {
            get
            {
                return _roleId;
            }
            set
            {
                _roleId = value;
            }
        }

        public static string UserId
        {
            get
            {
                return _userAccount;
            }
            set
            {
                _userAccount = value;
            }
        }
    }
}

